سلام چگونه میتونم در web api کاربر را احراز هویت کنم من از claaim استفاده کردم اما انجام نمی شود
var identity = new ClaimsIdentity(context.Options.AuthenticationType); identity.AddClaim(new Claim(ClaimTypes.Role, "user")); identity.AddClaim(new Claim("username", getUser.RealUserName)); identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, getUser.RealUserName)); identity.AddClaim(new Claim(ClaimTypes.Name, getUser.LastName)); var m = context.Validated(identity);
public class Startup { public void Configuration(IAppBuilder app) { app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); var myProvider = new HphAuthorization(); OAuthAuthorizationServerOptions options = new OAuthAuthorizationServerOptions { AllowInsecureHttp = true, TokenEndpointPath = new PathString("/m/token"), AccessTokenExpireTimeSpan = TimeSpan.FromDays(1), Provider = myProvider, AuthenticationMode = Microsoft.Owin.Security.AuthenticationMode.Active, }; app.UseOAuthAuthorizationServer(options); app.UseOAuthBearerAuthentication(new OAuthBearerAuthenticationOptions()); app.UseOAuthBearerTokens(options); HttpConfiguration config = new HttpConfiguration(); WebApiConfig.Register(config); } }
var testServer = TestServer.Create<Startup>(); var requestParams = new List<KeyValuePair<string, string>> { new KeyValuePair<string, string>("grant_type", "password"), new KeyValuePair<string, string>("username", username ), new KeyValuePair<string, string>("password",password) }; var requestParamsFormUrlEncoded = new FormUrlEncodedContent(requestParams); var tokenServiceResponse = await testServer.HttpClient.PostAsync( "/m/token", requestParamsFormUrlEncoded); var identity = (ClaimsPrincipal)System.Threading.Thread.CurrentPrincipal; var claims = identity.Claims;
باید از jwt استفاده کنید
هیچ کاربری تا کنون از این پست تشکر نکرده است
با ما تماس بگیرید تا در این مسیر همراهتان باشیم :)