首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Sharepoint 2013 -带有自定义登录页面的FBA和2FA

Sharepoint 2013 -带有自定义登录页面的FBA和2FA
EN

Stack Overflow用户
提问于 2017-05-26 10:44:58
回答 2查看 2.2K关注 0票数 0

我在Sharepoint是个完完全全的新手。两周前,我刚刚开始学习sharepoint,因为我的老板给我分配了一个sharepoint项目。我必须在一个现有的基于内部网的web应用程序中实现2FA和FBA。我认为这是一个简单的任务,仅仅通过研究,但我还没有找到一个明确的指南或回答我的问题。

以下是我的几项任务:

1)向站点添加基于表单的身份验证,并使用自定义登录页面。

2)认证

  • 登录时用AD检查用户名和密码。
  • 如果有效,则必须向第三方提供商请求OTP代码,以获得2FA。
  • 用户在通过这两个文件后都会进行身份验证。

配置和自定义登录页面并没有太大的麻烦,并且没有花很长时间就完成了。但我被困在了2FA部分。

1)如何定制身份验证过程?我不记得我从哪里得到了下面的代码,但我真的希望我能用它做点什么。所以,我能用它做点什么吗?不然我就走错路了?我真的很感谢你的帮助,谢谢你。

代码语言:javascript
运行
复制
protected void btnLogin_Click(object sender, EventArgs e)
    {
        bool status = SPClaimsUtility.AuthenticateFormsUser(
            Context.Request.UrlReferrer,
            txtUsername.Value.ToString(),
            txtPassword.Value.ToString());

        if (!status) // if auth failed
        {
            lblInvalid.InnerText = "Wrong Username or Password";
            lblInvalid.Visible = true;
        }
        else //if success
        {       
    //What do I do here to change the user back to not authenticated?   

        }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-05-29 21:17:54

正确登录后,设置联邦身份验证cookie域。

代码语言:javascript
运行
复制
HttpCookie httpCookie = current.Response.Cookies["FedAuth"];
httpCookie.Domain = "." + ConfigurationManager.AppSettings["yourdomain"];

签出方法比较复杂,很久以前我的解决方案是基于这个职位的。

以及基于sharepoint页面的签出方法(对变量名表示抱歉,但我正在解压缩旧的SignOut ),并从文章中修复:

代码语言:javascript
运行
复制
public static void SignOut(SPSite site, SPWeb web, IClaimsPrincipal principal)
{
    HttpContext current = HttpContext.Current;
    if (current.Session != null)
    {
        current.Session.Clear();
    }
    string value = string.Empty;
    if (current.Request.Browser["supportsEmptyStringInCookieValue"] == "false")
    {
        value = "NoCookie";
    }
    HttpCookie httpCookie = current.Request.Cookies["WSS_KeepSessionAuthenticated"];
    bool flag = false;
    for (int i = 0; i < current.Request.Cookies.Count; i++)
    {
        HttpCookie httpCookie2 = current.Request.Cookies.Get(i);
        if (httpCookie2.Name == "FedAuth" && !flag)
        {
            flag = true;
            httpCookie2.Domain =  WebConfigurationManager.AppSettings["yourdomain"];
        }
    }
    if (httpCookie != null)
    {
        httpCookie.Value = value;
        current.Response.Cookies.Remove("WSS_KeepSessionAuthenticated");
        current.Response.Cookies.Add(httpCookie);
    }
    HttpCookie httpCookie3 = current.Request.Cookies["MSOWebPartPage_AnonymousAccessCookie"];
    if (httpCookie3 != null)
    {
        httpCookie3.Value = value;
        httpCookie3.Expires = new DateTime(1970, 1, 1);
        current.Response.Cookies.Remove("MSOWebPartPage_AnonymousAccessCookie");
        current.Response.Cookies.Add(httpCookie3);
    }
    SPIisSettings iisSettingsWithFallback = site.WebApplication.GetIisSettingsWithFallback(site.Zone);
    if (iisSettingsWithFallback.UseClaimsAuthentication)
    {
        string iPUrl = Authentication.GetIPUrl(principal);
        if (iPUrl != string.Empty)
        {
            string str = HttpUtility.UrlEncode(SPContext.Current.Site.RootWeb.Url);
            string url = iPUrl + "?wa=wsignout1.0&wreply=" + str;
            FederatedAuthentication.SessionAuthenticationModule.SignOut();
            if (current.Session != null)
            {
                current.Session.Abandon();
            }
            current.Response.Redirect(url);
        }
        else
        {
            FederatedAuthentication.SessionAuthenticationModule.SignOut();
            int num = 0;
            foreach (SPAuthenticationProvider current2 in iisSettingsWithFallback.ClaimsAuthenticationProviders)
            {
                num++;
            }
            if (num != 1 || !iisSettingsWithFallback.UseWindowsIntegratedAuthentication)
            {
                if (current.Session != null)
                {
                    current.Session.Abandon();
                }
                SPUtility.Redirect(web.ServerRelativeUrl, 0, current);
                return;
            }
        }
    }
    if (AuthenticationMode.Forms == SPSecurity.AuthenticationMode)
    {
        FormsAuthentication.SignOut();
        if (current.Session != null)
        {
            current.Session.Abandon();
        }
        SPUtility.Redirect(web.ServerRelativeUrl, 0, current);
    }
    else if (AuthenticationMode.Windows != SPSecurity.AuthenticationMode)
    {
        throw new SPException();
    }
}

private static string GetIPUrl(IClaimsPrincipal principal)
{
    string result;
    if (principal == null)
    {
        result = string.Empty;
    }
    else
    {
        string text = string.Empty;
        try
        {
            string text2 = principal.Identity.Name.Split(new char[] {'|'})[1];
            if (SPSecurityTokenServiceManager.Local.TrustedLoginProviders[text2] != null)
            {
                text = SPSecurityTokenServiceManager.Local.TrustedLoginProviders[text2].ProviderUri.AbsoluteUri;
            }
        }
        catch (Exception ex)
        {
            // log
        }
        result = text;
    }
    return result;
}

进一步阅读:

票数 0
EN

Stack Overflow用户

发布于 2018-08-21 06:03:04

普通aspx页面

代码语言:javascript
运行
复制
<html>
<head>One Head</head>
<body>
    <form runat="server">
        <table>
            <tr>
                <td>User Name:</td>
                <td>
                    <asp:TextBox ID="txtUserName" runat="server" /></td>
            </tr>
            <tr>
                <td>Password:</td>
                <td>
                    <asp:TextBox ID="txtPassword" TextMode="Password" runat="server" /></td>
            </tr>
            <tr>
                <td colspan="2">
                    <asp:Button ID="btnButton" Text="Button" OnClick="btnButton_Click" runat="server" />
                </td>
            </tr>
        </table>
    </form>
</body>
</html>

您在添加Microsoft.SharePoint.identityModel时可能会遇到麻烦,这里是我得到的位置

代码语言:javascript
运行
复制
C:\Windows\Microsoft.NET\assembly\GAC_MSIL\Microsoft.SharePoint.IdentityModel\v4.0_15.0.0.0__71e9bce111e9429c\Microsoft.SharePoint.IdentityModel.dll

包含列表

代码语言:javascript
运行
复制
using System;
using Microsoft.SharePoint;
using System.DirectoryServices;
using System.DirectoryServices.AccountManagement;
using Microsoft.SharePoint.IdentityModel;
using System.IdentityModel.Tokens;

按钮单击代码

代码语言:javascript
运行
复制
protected void btnButton_Click(object sender, EventArgs e)
{
    string domn = "mydomain";

    string membershipProviderName = "membership";
    string roleProviderName = "rolemanager";
    string cookieeee = string.Format("{0}\\{1}", domn, txtUserName.Text);
    bool isAuthenticated = Authenticate(domn, txtUserName.Text, txtPassword.Text);
    if (isAuthenticated)
    {
        SecurityToken token = SPSecurityContext.SecurityTokenForFormsAuthentication(new Uri(SPContext.Current.Web.Url), 
            membershipProviderName, roleProviderName, txtUserName.Text, txtPassword.Text, 
            SPFormsAuthenticationOption.PersistentSignInRequest);
        SPFederationAuthenticationModule.Current.SetPrincipalAndWriteSessionToken(token);
        Response.Redirect("/");
    }
}

[DirectoryServicesPermission(System.Security.Permissions.SecurityAction.LinkDemand, Unrestricted = true)]
public static bool Authenticate(string domainName, string userAlias, string userPassword)
{
    try
    {
        PrincipalContext context = new PrincipalContext(ContextType.Domain, domainName);
        return context.ValidateCredentials(userAlias, userPassword, ContextOptions.Negotiate));
    }
    catch
    {
        throw;
    }
}

注意:确保在web配置文件中设置了所有配置。这只是自定义身份验证,如果在服务和web应用程序的中央管理和web配置中没有正确地设置角色和成员身份,这将无法工作。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/44199574

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档