gpt4 book ai didi

c# - 使用 OAuth 登录并从数据库中检索用户详细信息

转载 作者:太空宇宙 更新时间:2023-11-03 15:06:23 24 4
gpt4 key购买 nike

我是 OAuth 的新手。有谁知道如何使用 OAuth 登录,我设法通过传递用户名和密码生成了一个 token ,并在 javascript 中使用了 sessionStorage.setItem('accessToken') 来存储 token ,

 <script type="text/javascript">
$(document).ready(function () {
$('#btnLogin').click(function () {
$.ajax({
url: '/token',
method: 'POST',
contentType: 'application/json',
data: {
username: $('#txtEmail').val(),
password: $('#txtPassword').val(),
grant_type: 'password'
},
success: function (response) {

sessionStorage.setItem('accessToken', response.access_token);
sessionStorage.setItem('userName', response.userName);
window.location.href = "Jobs.html";

},
error: function (jqXHR) {
$('#divErrorText').text(jqXHR.responseText);
$('#divError').show('fade');
}
});
});
});

</script>

这是我自定义的 OAuthAuthorizationServerProvider

public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
var userManager = context.OwinContext.GetUserManager<ApplicationUserManager>();

//find a match with the entered username and password
AppUser user = await userManager.FindAsync(context.UserName, context.Password);
if(user == null)
{
context.SetError("Access Denied, Invalid Username or Password");
return;
}

//responsible to fetch the authenticated user identity from the database and returns an object of type “ClaimsIdentity”
ClaimsIdentity oAuthIdentity = await user.GenerateUserIdentityAsync(userManager, OAuthDefaults.AuthenticationType);
ClaimsIdentity cookieIdentity = 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(cookieIdentity);
}

如何让用户登录以及如何在不使用 sessionStorage.getItem 的情况下从数据库中检索用户名和电子邮件。我是否需要为此调用另一个 LogIn API? (像这样)

 public async Task<ActionResult> LogIn(LogInModel model)
{
if(!ModelState.IsValid)
{
return View();
}
userManager.FindAsync.
var user = await userManager.FindAsync(model.Email, model.Password);

// sign in the user using the cookie authentication middleware SignIn(identity)
if (user != null)
{
await SignIn(user);
return Redirect(GetRedirectUrl(model.ReturnUrl));
}

ModelState.AddModelError("", "Kas Error Message -Inavlid email or password");
return View();
}

private async Task SignIn(AppUser user)
{
var identity = await userManager.CreateIdentityAsync(
user, DefaultAuthenticationTypes.ApplicationCookie);

GetAuthenticationManager().SignIn(identity);
}

对不起,如果我听起来很厚 :) 感谢任何帮助

最佳答案

当您创建 Web API 项目并选择使用 ASP.Net Identity 时,它应该会生成一个包含您需要的 API 调用的 AccountController 类,例如 GetUserInfo()。看起来您必须执行单独的调用才能获取用户详细信息,不确定是否有一种简单的方法可以将执行登录然后返回用户详细信息的调用与访问 token 结合起来。

关于c# - 使用 OAuth 登录并从数据库中检索用户详细信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43350434/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com