我正在我的MVC 5应用程序中实现OAuth2身份验证,所以我可以在我的安卓应用程序中使用它。但我不确定这一切是如何进行的,因为我以前从未使用过Oauth2或任何令牌身份验证。
到目前为止,我在MVC中实现了折叠代码:
OwinContextExtensions.cs
public static class OwinContextExtensions
{
public static string GetUserId(this IOwinContext ctx)
{
var result = "-1";
var claim = ctx.Authentication.User.Claims.FirstOrDefault(c => c.Type == "UserID");
if (claim != null)
{
result = claim.Value;
}
return result;
}
}Startup.Auth.cs
public partial class Startup
{
public static OAuthAuthorizationServerOptions OAuthOptions { get; private set; }
static Startup()
{
OAuthOptions = new OAuthAuthorizationServerOptions
{
TokenEndpointPath = new PathString("/token"),
Provider = new OAuthAppProvider(),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(2),
AllowInsecureHttp = true
};
}
public void ConfigureAuth(IAppBuilder app)
{
app.UseOAuthBearerTokens(OAuthOptions);
}
}OAuthAppProvider.cs
public class OAuthAppProvider : OAuthAuthorizationServerProvider
{
private ProtokolEntities db = new ProtokolEntities();
public IFormsAuthenticationService FormsService { get; set; }
public IMembershipService MembershipService { get; set; }
public override Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
if (MembershipService == null) { MembershipService = new AccountMembershipService(); }
return Task.Factory.StartNew(() =>
{
var username = context.UserName;
var password = context.Password;
var userID = db.aspnet_Users.Where(x => x.UserName == username).SingleOrDefault().UserId.ToString();
if (MembershipService.ValidateUser(username, password))
{
var claims = new List<Claim>()
{
new Claim(ClaimTypes.Name, username),
new Claim("UserID", userID)
};
ClaimsIdentity oAutIdentity = new ClaimsIdentity(claims, Startup.OAuthOptions.AuthenticationType);
context.Validated(new AuthenticationTicket(oAutIdentity, new AuthenticationProperties() { }));
}
else
{
context.SetError("invalid_grant", "Error");
}
});
}
public override Task ValidateClientAuthentication(OAuthValidateClientAuthenticationContext context)
{
if (context.ClientId == null)
{
context.Validated();
}
return Task.FromResult<object>(null);
}
}Startup.cs.cs
[assembly: OwinStartup(typeof(PageOffice.Startup))]
namespace PageOffice
{
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
ConfigureAuth(app);
}
}
}当我在http://localhost:1076/token上打开邮递员并使用POST方法时,我原始地打开了Body ->并编写了
grant_type=password&password=mypassword&username=myusername
在那之后,我得到了这个结果:
{
"access_token": "QmmWSh4OZPfC8uv-jyzFzZx1KP05T8b09QlPP3Cy-_Zr9qvWtzWpxNTXOhc4U387N6VHNCnIPklgTEk8CISMyXlcsWAz7MxlRN8qI_Ajg8gjEphHUS1SrO0uDRG2XRqtX1gvTVupym_1xtsdjlwj2VXoc6ySvR0ihb2YjuXnSd4CNgKKaMBQLb1w8P1XB13jc4Pc5tump4-Y4dYn3A5hpvtc9fqpgVAUjZFdiJ_HXMiIpgmqdIFim0Ty8oRZolzpm3RSMPRV6ZIpZBqHG1A2kcdWN-52ZkHuL4_7U743vW0",
"token_type": "bearer",
"expires_in": 172799
}如何找到“客户端id”和“客户端机密”参数?另外,我真的不明白如何在安卓系统中使用这个access_token,在这教程上的解释是否足够?
谢谢你的阅读,很抱歉有这么多代码:)
发布于 2017-08-07 12:56:21
有一个关于用于本机应用程序的OAuth2的RFC。它建议使用“授权代码授权流”,但不使用客户端机密,因为您无法在应用程序二进制文件中保存该机密。
您现在使用的资源所有者密码凭据流(请参阅OAuth2 RFC),只应在资源所有者完全信任应用程序的罕见情况下使用,因为应用程序必须知道它们的密码。
在OAuth2提供程序注册应用程序(客户端)时,可以获得或指定客户端ID和机密。
您所链接的教程似乎讨论的是OAuth版本1,而不是2。
https://stackoverflow.com/questions/45546676
复制相似问题