asp.net - How to get oauth Authorization code in asp.net
问题描述
I am busy implementing an oauth
Authorization Server on my ASP.NET
WEB API.
I have setup the API to support authorization server:
Startup.Auth.cs
public void ConfigureAuth(IAppBuilder app)
{
app.CreatePerOwinContext(IdentityDbContext.Create);
app.CreatePerOwinContext<UserManager>(UserManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions());
app.UseExternalSignInCookie(DefaultAuthenticationTypes.ExternalCookie);
// Configure the application for OAuth based flow
PublicClientId = "self";
OAuthOptions = new OAuthAuthorizationServerOptions
{
AllowInsecureHttp = true,
ApplicationCanDisplayErrors = true,
TokenEndpointPath = new PathString("/Token"),
Provider = new SimpleAuthorizationServerProvider(),
RefreshTokenProvider = new SimpleRefreshTokenProvider(),
AccessTokenExpireTimeSpan = TimeSpan.FromHours(24),
AuthorizationCodeProvider = new SimpleAuthorizationCodeProvider()
};
app.UseOAuthBearerTokens(OAuthOptions);
app.UseOAuthAuthorizationServer(OAuthOptions);
}
I have then implemented my SimpleAuthorizationCodeProvider()
:
public class SimpleAuthorizationCodeProvider : IAuthenticationTokenProvider
{
public async Task CreateAsync(AuthenticationTokenCreateContext context)
{
context.Ticket.Properties.Dictionary.TryGetValue("as:client_id", out string clientId);
//Build holder object
var holder = new AuthorizationCodeHolder(0, clientId, context.SerializeTicket());
var protectedTicket = AesSymmetricalEncryption.EncryptData(Key, JsonConvert.SerializeObject(holder));
//Set the Token for the request
context.SetToken(protectedTicket);
}
public async Task ReceiveAsync(AuthenticationTokenReceiveContext context)
{
//Decrypt token
var token = AesSymmetricalEncryption.DecyptData(Key, context.Token);
//Convert back to object
var ticket = JsonConvert.DeserializeObject<AuthorizationCodeHolder>(token);
//
context.DeserializeTicket(ticket.Ticket);
}
However, now my issue is, I do not know how to generate this authorization code?
I have a section on a separate MVC site, where a user logs in. After they have done that, I need to return a response with a parameter code={auth code}
. How do I go about generating this authorization code?
Do I need to login to the api, and then call a specific endpoint? or can I just use the owin authentication in the MVC site to generate it?
I have tested the api, and I can login using an authentication code, but I just do not know how to generate a valid one?
解决方案
推荐阅读
- java - android studio中的空对象引用?
- r - 在 R 中创建 bib_text 引用?
- c++ - 运算符重载和函数重载产生不明确的编译器错误
- ruby - 正则表达式在 ruby 中提取字符串中的 iso 代码
- tensorflow - 如何在张量流数据集中定义自定义拆分?
- java - WireMock 测试用例在调用未定义时总是失败
- excel - Excel 文件设置为 UTF-8
- spring - 在 JobRegistry 中找不到作业
- javascript - 在映射 json 呈现的每个组件上调用 React Native onPress
- ansible - 如何指示 Ansible 远程节点命令在远程节点上查找文本资源文件