2017-04-11 75 views
2

我是OAuth的新用户。有谁知道如何使用OAuth登录,我已成功地产生通过将用户名和密码令牌,并在JavaScript中使用sessionStorage.setItem(“的accessToken”)来存储令牌,使用OAuth登录并从数据库检索用户详细信息

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

很抱歉,如果我听起来厚:)任何帮助表示赞赏

回答

0

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

相关问题