【发布时间】:2019-09-05 06:53:30
【问题描述】:
我有带有 WebApi 和个人用户帐户的 Asp.net Web 应用程序项目。 我实现了注册和登录,前端使用了angularjs。
现在,我需要在浏览器中存储 cookie。
如果基于 cookie 的身份验证将 cookie 存储在浏览器中,我感到很困惑?
在我的项目中,我根据令牌对用户进行身份验证。
我进行了研究,但它让我感到困惑,我没有找到使用 Asp.Net Identity webApi 存储 cookie 的明确指南。
这是用户身份验证的位置:
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
ApplicationUser user = await userManager.FindAsync(context.UserName, context.Password);
if (user == null)
{
context.SetError("invalid_grant", "The user name or password is incorrect.");
return;
}
/*
I tried the following but I get an error that Request doesn't exist
var claims = new List<Claim>();
claims.Add(new Claim(ClaimTypes.Name, context.UserName));
var id = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
var ctx = Request.GetOwinContext();
var authenticationManager = ctx.Authentication;
authenticationManager.SignIn(id);
*/
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager);
ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager);
AuthenticationProperties properties = CreateProperties(user.UserName);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
}
对不起,我是 Asp.net Identity 的新手。
如果在 WebApi (不是 MVC) 中的 Asp.Net Identity 中有明确的指南?
注意:我没有 LoginPath。
【问题讨论】:
-
我已经读过了,找不到我需要的。谢谢
标签: asp.net asp.net-web-api cookies