2015-03-23 3 views
0

Я пытаюсь реализовать идентификатор Asp.net в своем веб-приложении (MVC5). В методе аутентификации пользователя я аутентифицирую пользователя, если пользователь существует, я вручную создаю Identity and Principals в своем приложении (Authentication.cs). После этого я назначаю Принципам требований текущему HttpContext и Thread. Итак, теперь я могу видеть Identity в том же запросе httpcontext. Но если я пытаюсь получить доступ к Identity Identity из другого action/page, тогда он покажет нулевые значения в объекте User.Identity, то есть Identity не будет поддерживать последующий запрос.Asp.net Identity + Identity не поддерживается в последующем запросе

В моем приложении я установил AuthenticationMode = "None" в web.config, поэтому я удалил атрибут LoginPath из класса запуска OWIN.

Я не уверен, что я пропустил здесь, пожалуйста, помогите мне исправить эту проблему.

//OWIN startup.cs 
[assembly: OwinStartup(typeof(Web.Startup))] 
namespace Web 
{ 

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 
     app.UseCookieAuthentication(new CookieAuthenticationOptions 
     { 
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie, 
      CookieSecure = CookieSecureOption.Always 
     }); 

    } 
} 
} 


//Authentication.cs 
//After user authentication, setting Thread and http context. 
if (user != null) 
{ 
var authenticationManager = HttpContext.Current.GetOwinContext().Authentication; 

var claims = new List<Claim>(); 
claims.Add(new Claim(ClaimTypes.Name, "username")); 
claims.Add(new Claim(ClaimTypes.Email, "[email protected]")); 
var userIdentity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie); 

ClaimsPrincipal principal2 = new ClaimsPrincipal(userIdentity); 

authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, userIdentity); 

var test = HttpContext.Current.User.Identity.IsAuthenticated; //return false 


HttpContext.Current.User = principal2; 

test = userIdentity.IsAuthenticated; // User Identity is authenticated return true 
System.Threading.Thread.CurrentPrincipal = principal2; 
test = HttpContext.Current.User.Identity.IsAuthenticated; //return true 

}

Но если перенаправить на другую страницу/действие, то значения в User.Identity.IsAuthenticated отображает как ложь.

Благодаря Selvakumar

ответ