我正在尝试实施一个“记住账号”我的功能Web Api项目。
我想 :
- 有记住账号当用户使用时的功能Sign In.
- save a cookies用于保持用户始终登录,以便用户无需键入username and password他们每次访问网站时。
- 通过阅读以下内容使用户登录cookies上次登录时保存的。
我正在考虑的另一个问题是......我正在尝试生成cookies通过使用JavaScript
当用户检查记住账号复选框。是否有可能做到这一点?
OR
我应该实施RememberMe()
in the AccountController
??
添加:
这是我的代码ApplicationOAuthProvider
.
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();
ApplicationUser user = await userManager.FindByNameAsync(context.UserName);
if (user == null) {...}
if (userManager.IsLockedOut(user.Id)) {...}
if (!(await userManager.CheckPasswordAsync(user, context.Password)))
{ ... }
if (!user.EmailConfirmed) {...}
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager,
OAuthDefaults.AuthenticationType);
ClaimsIdentity cookiesIdentity = await user.GenerateUserIdentityAsync(userManager,
CookieAuthenticationDefaults.AuthenticationType);
AuthenticationProperties properties = CreateProperties(user.UserName);
AuthenticationTicket ticket = new AuthenticationTicket(oAuthIdentity, properties);
context.Validated(ticket);
context.Request.Context.Authentication.SignIn(cookiesIdentity);
在我的 JavaScript 中。
$('#checkbox').click(function () {
if ($('#checkbox').is(':checked')) {
// save username and password
username = $('#txtLoginEmail').val();
password = $('#pass').val();
checkbox = $('#chkRememberMe').val();
} else {
username = '';
password = '';
checkbox = '';
}
});
你需要实施刷新令牌在您的应用程序中能够提供此功能。
基本上,您需要创建一个RefreshTokenOAuthProvider
这将生成刷新令牌。您可以使用 2 种类型client_id
区分需要被记住或不需要被记住的客户。
它的解释是这一系列优秀的博客文章 http://bitoftech.net/2014/07/16/enable-oauth-refresh-tokens-angularjs-app-using-asp-net-web-api-2-owin/(虽然它可能开始变得有点过时,但有关 owin 设置的信息是黄金)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)