AuthenticationManager.SignIn() isn't present in AuthenticationManager class

后端 未结 1 472
一向
一向 2021-01-18 01:17

I\'m trying to use the method from the AuthenticationManager class SignIn();

Here is how I\'m doing it:

AuthenticationManag         


        
相关标签:
1条回答
  • 2021-01-18 02:01

    Simplified example of controller using IAuthenticationManager

    using Microsoft.Owin.Security;
    using System.Web;    
    //...other usings
    
    public class AccountController : Controller {
    
        [HttpPost]
        [ActionName("Login")]
        public ActionResult Login(LoginViewModel model) {
            if (ModelState.IsValid) {
                string userName = (string)Session["UserName"];
                string[] userRoles = (string[])Session["UserRoles"];
    
                ClaimsIdentity identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
    
                identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, userName));
    
                userRoles.ToList().ForEach((role) => identity.AddClaim(new Claim(ClaimTypes.Role, role)));
    
                identity.AddClaim(new Claim(ClaimTypes.Name, userName));
    
                AuthenticationManager.SignIn(identity);
                return RedirectToAction("Success");
            } else {
                return View("Login",model);
            }
        }
    
        private IAuthenticationManager AuthenticationManager {
            get {
                return HttpContext.GetOwinContext().Authentication;
            }
        }    
    }
    
    0 讨论(0)
提交回复
热议问题