【发布时间】:2017-12-07 10:03:02
【问题描述】:
类:启动
public class Startup
{
public void Configuration(IAppBuilder app)
{
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
// For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888
//enable cors origin requests
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
var myProvider = new ClinicServerProvider();
OAuthAuthorizationServerOptions options = new OAuthAuthorizationServerOptions
{
AllowInsecureHttp = true,
TokenEndpointPath = new PathString("/api/token"),
AccessTokenExpireTimeSpan = TimeSpan.FromDays(30),
Provider = myProvider
};
app.UseOAuthAuthorizationServer(options);
app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions());
HttpConfiguration config = new HttpConfiguration();
WebApiConfig.Register(config);
}
}
类:ClinicServerProvider
public class ClinicServerProvider : OAuthAuthorizationServerProvider
{
private readonly BoilerplateDbContext _context;
public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
{
context.OwinContext.Response.Headers.Add("Access-Control-Allow-Origin", new[] { "*" });
var identity = new ClaimsIdentity(context.Options.AuthenticationType);
UnitOfWork uow = new UnitOfWork(_context);
string epass = Crypto.EncryptString(context.Password);
var user = uow.UserRepository.Get(x => x.Password == epass && x.UserName == context.UserName && x.IsActive==true).FirstOrDefault();
// Other custom code here.
}
}
【问题讨论】:
-
你没有展示你实际上是如何调用那个 /token 的。
-
@Evk 我从邮递员那里打来电话,传递了访问详细信息。
-
@Evk snag.gy/ABWMdx.jpg 检查这个。以前工作,但不知道为什么它停止工作..现在密码为空。用户名来了。
标签: c# asp.net-web-api token asp.net-authorization