Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用OAuth&;从数据库检索用户详细信息_C#_Asp.net_Api_Oauth_Oauth 2.0 - Fatal编程技术网

C# 使用OAuth&;从数据库检索用户详细信息

C# 使用OAuth&;从数据库检索用户详细信息,c#,asp.net,api,oauth,oauth-2.0,C#,Asp.net,Api,Oauth,Oauth 2.0,我是OAuth的新手。有人知道如何使用OAuth登录吗?我已经通过传递用户名和密码生成了一个令牌,并使用javascript中的sessionStorage.setItem('accessToken')来存储令牌 <script type="text/javascript"> $(document).ready(function () { $('#btnLogin').click(function () { $.ajax({

我是OAuth的新手。有人知道如何使用OAuth登录吗?我已经通过传递用户名和密码生成了一个令牌,并使用javascript中的sessionStorage.setItem('accessToken')来存储令牌

 <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>

$(文档).ready(函数(){
$('#btnLogin')。单击(函数(){
$.ajax({
url:“/token”,
方法:“POST”,
contentType:'应用程序/json',
数据:{
用户名:$('#txtEmail').val(),
密码:$('#txtPassword').val(),
授权类型:“密码”
},
成功:功能(响应){
sessionStorage.setItem('accessToken',response.access\u token);
setItem('userName',response.userName);
window.location.href=“Jobs.html”;
},
错误:函数(jqXHR){
$('#diversorText').text(jqXHR.responseText);
$('diveror')。显示('fade');
}
});
});
});
这是我的自定义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);
    }
public override异步任务GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext)
{
var userManager=context.OwinContext.GetUserManager();
//查找与输入的用户名和密码匹配的项
AppUser=await userManager.FindAsync(context.UserName,context.Password);
if(user==null)
{
SetError(“访问被拒绝,用户名或密码无效”);
返回;
}
//负责从数据库中获取经过身份验证的用户标识,并返回“ClaimSideEntity”类型的对象
ClaimsIdentity oAuthIdentity=await user.GenerateUserIdentityAsync(userManager,OAuthDefaults.AuthenticationType);
ClaimSideEntity cookieIdentity=Wait user.GenerateUserIdentityAsync(userManager,CookieAuthenticationDefaults.AuthenticationType);
AuthenticationProperties=CreateProperties(user.UserName);
AuthenticationTicket=新的AuthenticationTicket(OAuthidentitity,属性);
上下文。已验证(票证);
context.Request.context.Authentication.sign(cookieIdentity);
}
如何登录用户以及如何在不使用sessionStorage.getItem的情况下从DB检索用户名和电子邮件。我需要为此调用另一个登录API吗?(类似这样的)

公共异步任务登录(LogInModel模型)
{
如果(!ModelState.IsValid)
{
返回视图();
}
userManager.FindAsync。
var user=await userManager.FindAsync(model.Email,model.Password);
//使用cookie身份验证中间件SignIn(identity)登录用户
如果(用户!=null)
{
等待登录(用户);
返回重定向(GetRedirectUrl(model.ReturnUrl));
}
AddModelError(“,“Kas错误消息-Inavlid电子邮件或密码”);
返回视图();
}
专用异步任务登录(AppUser)
{
var identity=await userManager.createidentitysync(
用户,DefaultAuthenticationTypes.ApplicationOkie);
GetAuthenticationManager().SignIn(标识);
}

很抱歉,如果我听起来很粗俗:)如果您创建了一个Web API项目并选择使用ASP.Net Identity,那么它应该生成一个AccountController类,其中包含您需要的API调用,例如
GetUserInfo()
,我们将非常感谢您的帮助。看起来您必须执行单独的调用才能获取用户详细信息,但不确定是否有一种简单的方法可以组合调用以执行登录,然后将用户详细信息与访问令牌一起返回

 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);
    }