2015-09-04 38 views
0

我在登錄頁面實現了owin身份驗證,但是我想在asp.net vnext(MVC6)中使用owin身份驗證的中間件,因爲我想在用戶登錄狀態保持很長時間時重定向到默認(登錄)頁面。下面的代碼是參考資料。如何在asp.net vnext(MVC 6)中創建/創建owin認證中間件?

var claims = new List<Claim> 
        { 
         new Claim("UserId",user.Id), 
         new Claim("UserName",user.UserName), 
         new Claim("Roles",user.UserRoles) 
        }; 
        var ident = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie,user.UserName,user.UserRoles); 
        var authProp = new AuthenticationProperties() {IsPersistent=true,AllowRefresh=true,ExpiresUtc=System.DateTime.Now.AddMinutes(1).ToLocalTime(),RedirectUri="/Account/Login"}; 
        IOwinContext owinContext = new OwinContext(); 
        owinContext.Authentication.SignIn(authProp, ident); 

在此先感謝。

回答

0

如果您正在尋找默認登錄頁面(如果用戶未登錄),則可以使用控制器上方的[授權]屬性。

namespace Demo.Controllers 
{ 
[Authorize] 
public class DemoController : Controller 
{ 
    // Your Code 
} 
} 

這應該對你有幫助..!

+0

謝謝你,分享你對問題的看法。但我希望用戶憑證的中間件能夠被mvc6應用程序的任何頁面動態捕獲,並且如果用戶長時間登錄,所以我想重定向默認頁面意味着在mvc6中註銷頁面。 所以這是我在開發mvc6項目時遇到的問題。 如果你知道如何解決這個問題,請分享你的想法,我很欣賞你的想法。 – johnkhadka

相關問題