Asp.net mvc 5 如何在MVC5 web应用程序和web API 2应用程序之间共享访问令牌
在本例中,我让用户登录(MVC5)Web应用程序,然后该应用程序作为代理登录(WebAPI2)API(使用basic over SSL)并返回承载/访问令牌。我正在使用Thinktecture.IdentityModel.Client.OAuth2Client来处理登录并获取访问令牌,这一切都很正常 发生了一些其他事情,但现在我希望Web应用程序能够解码访问令牌,以访问API上设置的声明(特别是登录API后返回的用户ID) 我正在使用大量演示的,Asp.net mvc 5 如何在MVC5 web应用程序和web API 2应用程序之间共享访问令牌,asp.net-mvc-5,access-token,asp.net-web-api2,jwt,oauth2client,Asp.net Mvc 5,Access Token,Asp.net Web Api2,Jwt,Oauth2client,在本例中,我让用户登录(MVC5)Web应用程序,然后该应用程序作为代理登录(WebAPI2)API(使用basic over SSL)并返回承载/访问令牌。我正在使用Thinktecture.IdentityModel.Client.OAuth2Client来处理登录并获取访问令牌,这一切都很正常 发生了一些其他事情,但现在我希望Web应用程序能够解码访问令牌,以访问API上设置的声明(特别是登录API后返回的用户ID) 我正在使用大量演示的,useAuthAuthorizationServe
useAuthAuthorizationServer
和useAuthBeareAuthentication
扩展方法和令牌端点(非常现成),但使用自定义的OAuthAuthorizationServerOptions.Provider
来访问我自己的存储库
我在两个应用程序上都有相同的MachineKey,但我不清楚如何解码令牌,尽管我知道我可能必须使用SecureDataFormat.Unprotect
方法
我在Web应用程序中最接近的尝试是:
Task<TokenResponse> response = client.RequestResourceOwnerPasswordAsync(model.Email, model.Password);
IDataProtector dataProtecter = Startup.DataProtectionProvider.Create("does this matter?");
TicketDataFormat ticketDataFormat = new TicketDataFormat(dataProtecter);
AuthenticationTicket ticket = ticketDataFormat.Unprotect(response.Result.AccessToken);
我的后备计划是提供一个API方法,该方法在登录后返回我感兴趣的信息,但这似乎有些过分,因为它构成了令牌中声明的一部分(据我所知)
我曾试着去了解JWT(我看过Thinktecture、Microsoft源代码和其他各种论坛),但不确定这是否会有帮助(尽管以纯文本形式提供的声明很有用)。我还没有找到一个允许使用基本身份验证登录并返回包含访问令牌的自定义JWT的示例
无论如何,我希望这是enoguh的信息和任何帮助都会非常感谢。。。干杯如果您正在使用UseAuthBeareAuthentication,则可以在Startup.Auth.cs类中将OAuthBeareOptions设置为静态:
public partial class Startup
{
public static OAuthBearerAuthenticationOptions OAuthBearerOptions { get; private set; }
...
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
app.UseOAuthBearerAuthentication(OAuthBearerOptions);
...
}
}
现在,您可以使用票据访问API用户的声明:
ticket.Identity.Claims
希望这能回答你的问题
编辑
这将解决您的问题,请看一看。你们非常接近。沙迪在正确的轨道上。即使这是一个单独的mvc应用程序,您仍然需要如图所示解密令牌并提取声明。如果web api令牌位于名为accessToken的变量中,则可以在mvc web应用程序中执行以下操作。(注意,mvc同时使用承载身份验证和cookie身份验证,OAuthBeareOptions是一个静态类) 下面是startup.auth.cs应该包括的内容(注意,我们有一个OAuthBeareAuthenticationOptions静态成员,我们称之为app.useAuthBeareAuthentication(),以便能够解密承载令牌
public static OAuthBearerAuthenticationOptions OAuthBearerOptions { get; private set; }
static Startup()
{
OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
}
public void ConfigureAuth(IAppBuilder app)
{
app.UseOAuthBearerAuthentication(OAuthBearerOptions);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});
}
Hi-Shadi,thansk对于响应,是的,我已经使用该方法设置了我的安全性。我实际尝试的是从一个完全独立的MVC应用程序中解密令牌。您尝试解密令牌的应用程序是什么类型的?它是类库、服务、Web Api、Java吗?Hi-Shadi根据上面的描述(MVC 5)我忘了说我更新了我的答案,我指向了另一个问题。嗨,沙迪,我担心这个问题与另一个问题有关。事实上,我以前见过这个问题,我也发表了反对它的回复。
ticket.Identity.Claims
//Unprotect the token
var unencryptedToken = Startup.OAuthBearerOptions.AccessTokenFormat.Unprotect(accessToken);
//Extract the claims identity from the token
var identity = unencryptedToken.Identity;
//Once you have the claims identity extracted, create a new claims identity that uses
//ApplicationCookie as the default authentication type
var id = new ClaimsIdentity(identity.Claims, DefaultAuthenticationTypes.ApplicationCookie);
// Now you are ready to sign in with your new claims identity. You basically created an
//application cookie from a bearer token and now using this cookie to sign-in
AuthenticationManager.SignIn(id);
public static OAuthBearerAuthenticationOptions OAuthBearerOptions { get; private set; }
static Startup()
{
OAuthBearerOptions = new OAuthBearerAuthenticationOptions();
}
public void ConfigureAuth(IAppBuilder app)
{
app.UseOAuthBearerAuthentication(OAuthBearerOptions);
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
});
}