簡體   English   中英

如何手動設置登錄的身份用戶?

[英]How do you manually set a logged in Identity User?

我正在使用ASP.NET身份與ADFS服務器。 出於開發目的,我想避免在我無法訪問ADFS服務器的網絡環境中使用ADFS服務器。 這就是為什么我在我的HomeController中添加了一個簡單的控制器動作,它手動設置當前登錄的用戶:

#if DEBUG
    [AllowAnonymous]
    public ActionResult LogIn()
    {
        var claims = new List<Claim>();
        claims.Add(new Claim(ClaimTypes.NameIdentifier, "tester"));

        System.Web.HttpContext.Current.User = new ClaimsPrincipal(new ClaimsIdentity(claims));
        System.Threading.Thread.CurrentPrincipal = System.Web.HttpContext.Current.User;

        return Redirect("Home/Index");
    }
#endif

和Owin配置方法:

public void Configuration(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions() { });

        app.UseWsFederationAuthentication(
            new WsFederationAuthenticationOptions
            {
                Wtrealm = realm,
                MetadataAddress = adfsMetadata
            });
    }

注釋掉我使用WsFederation身份驗證的部分沒有問題,這樣就沒有鏈接到我當前的ADFS服務器。

問題:當我被重定向到Home / Index操作(具有Authorize屬性)時,ASP.NET Identity無法將我的ClaimsPrincipal識別為有效登錄,因此我被重定向到Home / Login操作,在Home / Login和Home / Index之間不斷創建一個循環。

我的問題:如何讓ASP.NET接受上面創建的ClaimsPrincipal作為有效登錄?

您遇到問題 - 未設置cookie,因此不會在HTTP請求中保留用戶信息。 您的方法僅適用於一次調用(有用途,但不適用於您)

您仍然可以使用OWIN的IAuthenticationManager來設置cookie:

#if DEBUG
    [AllowAnonymous]
    public ActionResult LogIn()
    {
        var identity = new ClaimsIdentity("ApplicationCookie", ClaimsIdentity.DefaultNameClaimType, ClaimsIdentity.DefaultRoleClaimType);
        identity.AddClaim(new Claim("http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider", "Active Directory"));
        identity.AddClaim(new Claim(ClaimTypes.Name, "Testy McTestface"));
        identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "testUser"));

        IAuthenticationManager authenticationManager = HttpContext.GetOwinContext().Authentication;
        authenticationManager.SignOut("ApplicationCookie");
        authenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = false }, identity);

        return Redirect("Home/Index");
    }
#endif

您將需要nuget包Microsoft.Owin.Security.CookiesMicrosoft.Owin.Host.SystemWeb 在我的博客文章中查看有關AD身份驗證的更多解釋

您還需要確保CookieAuthenticationMiddleware配置為correclty:

 public void Configuration(IAppBuilder app)
    {
        app.SetDefaultSignInAsAuthenticationType(CookieAuthenticationDefaults.AuthenticationType);

        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = "ApplicationCookie",
            LoginPath = new PathString("/Home/Login"),
            Provider = new CookieAuthenticationProvider(),
            CookieName = "ApplicationCookie",
            CookieHttpOnly = true,
            ExpireTimeSpan = TimeSpan.FromHours(1),
        });

        app.UseWsFederationAuthentication(
            new WsFederationAuthenticationOptions
            {
                Wtrealm = realm,
                MetadataAddress = adfsMetadata
            });
    }

特別是對AuthenticationType值進行AuthenticationType - 它必須與ClaimsIdentity構造函數中的值匹配。 否則將不會設置cookie,否則您將無法注銷。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM