我有一个身份问题,我不太熟悉它。不久前,我开始了一个新的项目,最初并不打算附加任何身份验证。然而,随着项目的发展,我们发现我们应该实施它。因为最初不是这样设置的,所以我创建了一个登录表单。
我找到了这个问题的答案并付诸实施:
How to implement custom authentication in ASP.NET MVC 5
但是它不起作用,我不知道为什么。
这是我的代码:
这里没什么好看的,只是一个简单的形式。

@{
    ViewBag.Title = "Login";
}

<div class="container">

<h2>Login</h2>

<br />

@using (Html.BeginForm("Login", "Main"))
{
    <div class="row">
        <div class="form-group col-xs-6">
            @Html.Label("Username", htmlAttributes: new { @class = "control-label col-sm-3" })
            <div class="col-sm-8">
                @Html.TextBox("username", null, new { @class = "form-control" })
                @*Html.ValidationMessageFor(model => model.EnrollmentOption, "", new { @class = "text-danger" })*@
            </div>
        </div>

        <div class="form-group col-xs-6">
        </div>

    </div>

    <div class="row">
        <div class="form-group col-xs-6">
            @Html.Label("Password", htmlAttributes: new { @class = "control-label col-sm-3" })
            <div class="col-sm-8">
                @Html.Password("password", null, new { @class = "form-control" })
                @*Html.ValidationMessageFor(model => model.EffectiveDate, "", new { @class = "text-danger" })*@
            </div>
        </div>

        <div class="form-group col-xs-6">
        </div>

    </div>

    <div class="row">
        <div class="form-group">
            <div class="col-md-offset-6 col-sm-5">
                <input type="submit" id="login" value="Sign in" class="btn btn-primary" />
            </div>
        </div>
    </div>
}

为此而采取的行动就是这样,这一点更为重要:
    [HttpPost]
    public ActionResult Login(string username, string password)
    {
        if (isLoginValid(username, password))
        {
            var ident = new ClaimsIdentity(
              new[] {
          // adding following 2 claim just for supporting default antiforgery provider
          new Claim(ClaimTypes.NameIdentifier, username),
          new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "ASP.NET Identity", "http://www.w3.org/2001/XMLSchema#string"),

          new Claim(ClaimTypes.Name,username),

          // No roles needed for now...
              },
              DefaultAuthenticationTypes.ApplicationCookie);

            HttpContext.GetOwinContext().Authentication.SignIn(
               new AuthenticationProperties { IsPersistent = false }, ident);
            return RedirectToAction("QuoteSearch"); // auth succeed
        }
        else
        {
            // invalid username or password
            ModelState.AddModelError("", "Invalid username or password");
            return View();
        }
    }

还有isloginvalid函数(现在设置为使用硬编码登录)
    [NonAction]
    private bool isLoginValid(string user, string password)
    {
        return (user.ToLower() == "someUser" && password.ToLower() == "myPassword");
    }

我不太了解这种说法,也不知道身份在背后是如何运作的。不过,我确实确保添加了所有必要的参考资料。当在登录重定向后对我的操作使用authorize属性时,我收到来自IIS的未经授权的请求。我的代码有问题吗?
我应该更改或修复什么才能使用身份的授权部分?
谢谢,

最佳答案

经过一些故障排除和感谢盖舍曼的意见,我能够找到解决办法。由于我在创建解决方案时没有考虑身份验证,因此我删除了app_start文件夹中包含的引用和必要的owin配置代码。
因此,身份中没有定义任何内容,授权部分也没有发生任何内容,尽管登录工作正常。通过创建一个新项目并添加配置标识所需的所有代码,我可以正确地使用authorize属性。没有任何问题。
(这个答案适用于asp.net 4.6,我想对于asp.net core,处理方式有所不同)
更新:
为了更好地回答这个问题,我想我应该更详细地说明我做了些什么来让它起作用。
使用标识创建新项目时,您将看到有几个文件未创建如果选择不添加,则需要这些文件,其中大多数文件存储在app_start中。
c# - 创建没有身份验证的项目时的ASP.NET身份集成-LMLPHP
我复制了我没有的文件,并更改了名称空间以匹配我的实际项目。一旦这样做,就可以清楚地知道我丢失了哪些nuget包,所以我添加了那些我还没有添加的包。
startup.auth.cs将定义用于标识的密钥函数:
配置身份验证
必须在启动类中调用此函数。在配置方法中。
c# - 创建没有身份验证的项目时的ASP.NET身份集成-LMLPHP
c# - 创建没有身份验证的项目时的ASP.NET身份集成-LMLPHP
最后,要使一切正常工作,还必须包含通常在models文件夹中创建的文件identitymodel.cs。在我的例子中,我把所有的模型放置在一个不同的项目中,所以我把类放在那里,并在IdditTyf.C.CS上添加了引用,这样类就可以识别身份模型存在。
c# - 创建没有身份验证的项目时的ASP.NET身份集成-LMLPHP
就这样。在我的例子中,identity尝试连接到数据库以查找用户时遇到了很多问题,因为identity没有配置数据库,所以我的应用程序由于数据库连接失败而开始崩溃。去掉第三幅图中用红色标记的线使它对我起作用。我不想要identity的db连接,因为我有自己的用户处理,这可能不是其他人的情况。

07-25 23:42
查看更多