簡體   English   中英

ASP.Net Core 2身份驗證

[英]ASP.Net Core 2 authentication

我迷失於ASP.Net Core 2 MVC應用程序中的身份驗證。 我正在使用Core 2版本,似乎在版本1和版本2之間有很多更改。我已經閱讀了一些實際上沒有用的教程。

首先,這是我在ConfigureServices()方法中放入Startup.cs的內容

services.AddIdentity<MyUserClass, IdentityRole>()
                .AddEntityFrameworkStores<MyDatabaseEFContext>()
                .AddDefaultTokenProviders();

services.ConfigureApplicationCookie(options =>
            {
                // Cookie settings
                options.Cookie.HttpOnly = true;
                options.Cookie.Expiration = TimeSpan.FromDays(150);
                options.LoginPath = "/Account/Login"; // If the LoginPath is not set here, ASP.NET Core will default to /Account/Login
                options.LogoutPath = "/Account/Logout"; // If the LogoutPath is not set here, ASP.NET Core will default to /Account/Logout
                options.AccessDeniedPath = "/Account/AccessDenied"; // If the AccessDeniedPath is not set here, ASP.NET Core will default to /Account/AccessDenied
                options.SlidingExpiration = true;
            });

這是我放入Configure()方法的內容:

app.UseIdentity();

我將此注釋放在每個控制器的每個操作方法上:

[Authorize]

這是我在操作后登錄方法中所做的事情:

[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Index(LoginViewModel model, string returnUrl)
{
    if (!ModelState.IsValid)
    {
        return View(model);
    }

    var claims = new List<Claim> {new Claim(ClaimTypes.Name, model.Login)};
    var identity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
    var principal = new ClaimsPrincipal(identity);

    await HttpContext.Authentication.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, principal);

    return RedirectToAction("Index", "PrivateController");
}

嘗試登錄時出現此異常:

InvalidOperationException:未配置身份驗證處理程序來處理方案:Cookies

知道什么是錯的嗎?

在您的Configure()方法中,將app.UseIdentity()更改為:

app.UseAuthentication();

另外,請注意 :如果您使用的是不帶身份的Cookie(如“ Index操作中所示):

ConfigureServices方法中調用AddAuthenticationAddCookie方法:

 // If you don't want the cookie to be automatically authenticated and assigned to HttpContext.User, // remove the CookieAuthenticationDefaults.AuthenticationScheme parameter passed to AddAuthentication. services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme) .AddCookie(options => { options.LoginPath = "/Account/LogIn"; options.LogoutPath = "/Account/LogOff"; }); 

其他閱讀:將身份驗證和身份遷移到ASP.NET Core 2.0

我以自己執行Logout()動作的方式修復了該問題,該動作刪除了身份驗證Cookie,然后重定向到起始頁。 為了可靠地做到這一點,我給了認證。 使用Startup.csConfigureServices()方法對我自己的名稱進行cookie。

Startup.cs:

    private void ConfigureServices(IServiceCollection services)
    {
        ..

        services.ConfigureApplicationCookie(options =>
        {
            // Cookie settings
            options.Cookie.HttpOnly = true;
            options.ExpireTimeSpan = TimeSpan.FromMinutes(30);

            options.LoginPath = "/Identity/Account/Login";
            options.AccessDeniedPath = "/Identity/Account/AccessDenied";
            options.SlidingExpiration = true;
            options.Cookie.Name = "MyOwnCookieName";
        });
        ...

HomeController.cs:

    [Authorize]
    [HttpGet]
    public IActionResult Logout()
    {
        Response.Cookies.Delete("MyOwnCookieName");
        return RedirectToAction("Index");
    }

也許這為我節省了一些時間,因為我花了很多時間到達那里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM