簡體   English   中英

自定義OWIN身份驗證

[英]Custom OWIN authentication

我正在嘗試使用OWIN對MVC5進行自己的身份驗證。 我想避免使用標准的.NET身份+ EF,因為我要重寫現有網站的Web層,並使基礎數據庫保持不變(該數據庫使用自定義表單身份驗證提供程序,用於密碼的bcrypt等)。 。 我目前無法讓我的用戶通過身份驗證。 這是我目前擁有的:

Startup.cs:

public partial class Startup
{
    public void Configuration(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions()
        {
            AuthenticationType = CookieAuthenticationDefaults.AuthenticationType,
            LoginPath = new PathString("/account/login")
        });
    }
}

AccountController.cs:

public class AccountController : Controller
{
    private IAuthenticationManager authenticationManager
    {
        get
        {
            return this.HttpContext.GetOwinContext().Authentication;
        }
    }

    [AllowAnonymous]
    public ActionResult Login(string returnUrl)
    {
        ViewBag.ReturnUrl = returnUrl;
        return View();
    }

    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public ActionResult Login(LoginViewModel model)
    {
        if (!ModelState.IsValid)
            return View();

        var identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
        identity.AddClaim(new Claim(ClaimTypes.Name, model.Email));
        identity.AddClaim(new Claim(ClaimTypes.Email, model.Email));

        this.authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = model.RememberMe }, identity);

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

    public ActionResult LogOff()
    {
        this.authenticationManager.SignOut();

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

現在,我的“主頁/索引”操作裝飾有[Authorize]屬性。 這似乎起作用,因為當我導航到該頁面時,我被推送到登錄頁面。 這將正確發回並調用IAuthenticationManager.SignIn方法,然后將我重定向到主頁。 但是,在這一點上,我只是簡單地再次重定向回登錄頁面,這表明我的用戶尚未實際登錄。 我已經編寫了自己的WebAPI身份驗證處理程序(標頭中的API密鑰身份驗證等),它們是自定義的中間件,但是網上的大量信息表明,以下內容對於MVC中的身份驗證就足夠了。 有什么想法我可能會出錯嗎?

我寫這本書不久,立即發現了這個問題:我的身份驗證類型在OWIN啟動類和帳戶控制器中是不同的。 更改為相同,效果完美。

暫無
暫無

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

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