java 令牌_WEB API - 使用Bearer令牌进行身份验证

我创建了一个允许外部认证/注册的MVC应用程序 . 它创建了所有必要的组件(Owin,EF,Regiter,Login,Logout),我能够在应用程序中执行所有基本活动 .

现在,我想将Web应用程序与我的移动应用程序将使用的WEB API集成 . 我坚持使用web api调用中的身份验证(使用从Web应用程序收到的持有者令牌) .

我看到了创建启用了OWIN中间件的WEB API项目的示例 . 但我不知道如何集中外部身份验证过程并将令牌用于我的Web应用程序和移动应用程序和 I don't want to go for the ANGULAR 或单页应用程序 . 任何人都可以建议我正确的技术路径来解决这个问题 . 谢谢 .

Step 1:

我在visual studio 2015中创建了一个MVC项目,启用了Individual Login . 并配置了我在谷歌开发者控制台中配置所有内容的密钥 . 我的 Startup.cs 将具有以下代码

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.Create);

app.CreatePerOwinContext(ApplicationSignInManager.Create);

// Enable the application to use a cookie to store information for the signed in user

// and to use a cookie to temporarily store information about a user logging in with a third party login provider

// Configure the sign in cookie

app.UseCookieAuthentication(new CookieAuthenticationOptions

{

AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,

LoginPath = new PathString("/Account/Login"),

Provider = new CookieAuthenticationProvider

{

// Enables the application to validate the security stamp when the user logs in.

// This is a security feature which is used when you change a password or add an external login to your account.

OnValidateIdentity = SecurityStampValidator.OnValidateIdentity(

validateInterval: TimeSpan.FromMinutes(30),

regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))

}

});

Step 2:

更改了webconfig文件以指向我的本地数据库并运行应用程序,我能够使用我的gmail帐户成功登录google并将用户详细信息成功添加到数据库中的ASPUSerTables

Step 3:

现在我想创建一个WEB API项目,它将连接到数据库并将一些数据反转到MVC Web应用程序和移动应用程序(我在这里停留在认证部分) . 我也需要对我的移动应用程序使用第三方身份验证(Xamarin)并使用我的移动应用程序和MVC网站上的通用API

Step 4 所以我想,我应该创建WEB API项目,而不是WEB应用程序(步骤1),它将如下所示返回 Startup.cs 中的auth令牌并将该cookie存储在网站中以传递后续请求 .

app.UseCookieAuthentication(new CookieAuthenticationOptions());

app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);

// Configure the application for OAuth based flow

PublicClientId = "self";

OAuthOptions = new OAuthAuthorizationServerOptions

{

TokenEndpointPath = new PathString("/Token"),

Provider = new ApplicationOAuthProvider(PublicClientId),

AuthorizeEndpointPath = new PathString("/api/Account/ExternalLogin"),

AccessTokenExpireTimeSpan = TimeSpan.FromDays(14),

// In production mode set AllowInsecureHttp = false

AllowInsecureHttp = true

};

我不想使用ANGULAR,我需要我的WebApplication(MVC)和WEB API项目正确地对所有请求进行身份验证 . 请告诉我正确的道路 . 谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值